/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package com.android1.amarena2d.nodes;

import com.android1.amarena2d.engine.AmarenaCamera;
import com.android1.amarena2d.engine.AmarenaEngine;
import com.android1.amarena2d.input.InverseScrollController;
import com.android1.amarena2d.transition.Transition;

public class Scene extends LayerNode {

    public static boolean CULLING;
    public static AmarenaCamera CAM;

    Transition transition;
    boolean disposeOnExit = true;
    boolean culling;


    @Override
    public void render() {
        //Fast access during rendering.
        Scene.CULLING = culling;
        Scene.CAM = getCamera();
        super.render();
    }

    protected void onEnterFinished() {

    }

    protected void onExitFinished() {

    }

    @SuppressWarnings("unchecked")
    public LayerNode createLayer() {
        LayerNode layerNode = new LayerNode();
        add(layerNode);
        return layerNode;
    }

    public Transition getTransition() {
        return transition;
    }

    public void setTransition(Transition transition) {
        this.transition = transition;
    }

    public void createCameraDragController() {
        touch().makeTouchable(new InverseScrollController(engine.getCamera())).setTranslateToScreen();
    }

    @Override
    public void exit() {
        if (disposeOnExit)
            dispose();
        super.exit();
        onExitFinished();
    }

    @Override
    public void enter() {
        AmarenaEngine e = engine;
        super.enter();
        onEnterFinished();
    }

    @Override
    public void dispose() {
        AmarenaEngine e = engine;
        super.dispose();
        //makes sure we free up leaked resources
        engine.getResourceManager().disposeAll();
    }

    @Override
    public float getAbsoluteY() {
        return getY();
    }

    @Override
    public float getAbsoluteX() {
        return getX();
    }

    @Override
    public void setParent(NodeEntity parent) {
        super.setParent(parent);
    }

    public void setDisposeOnExit(boolean disposeOnExit) {
        this.disposeOnExit = disposeOnExit;
    }

    public boolean isDisposeOnExit() {
        return disposeOnExit;
    }

    @Override
    public AmarenaCamera getCamera() {
        return engine.getRenderer().getCamera();
    }
}
